package com.java2007.service.impl;

import com.java2007.mapper.ReaderCardMapper;
import com.java2007.mapper.ReaderInfoMapper;
import com.java2007.pojo.ReaderInfo;
import com.java2007.service.ReaderInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author zy
 * @version V1.0
 * @date 2021/03/10
 */
@Service
@Transactional
public class ReaderInfoServiceImpl implements ReaderInfoService {
    @Autowired
    private ReaderInfoMapper readerInfoMapper;

    @Autowired
    private ReaderCardMapper readerCardMapper;

    @Override
    public List<ReaderInfo> findAll() {
        return readerInfoMapper.findAll();
    }

    @Override
    public void add(ReaderInfo readerInfo) {
        readerInfoMapper.add(readerInfo);
        readerInfo.setReaderName("r" + readerInfo.getReaderId());
        readerCardMapper.addByReaderInfo(readerInfo);
    }

    @Override
    public boolean delete(Long readerId) {
        boolean res = readerInfoMapper.deleteByReaderInfoId(readerId);
        if (res == true){
            readerCardMapper.deleteById(readerId);
        }
        return res;
    }

    @Override
    public ReaderInfo findByReaderId(Long readerId) {
        return readerInfoMapper.findByReaderId(readerId);
    }

    @Override
    public void edit(ReaderInfo readerInfo) {
        readerInfoMapper.update(readerInfo);
    }

}
